package org.flower.dao.impl;

import org.flower.dao.CarouselDao;
import org.flower.entity.Carousel;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 轮播图实体类
 */
public class CarouselDaoImpl extends BaseDao implements CarouselDao {

    @Override
    public List<Carousel> getCarByPage(String page){
        List<Carousel>  carlist = new ArrayList<Carousel>();
        String sql = "select * from Carousel where page=? order by SerialNum";
        ResultSet rs = super.executeQuery(sql,page);
        try {
            while (rs.next()){
                Carousel car = new Carousel(rs.getInt("id"),rs.getString("image"),
                        rs.getString("ahref"),rs.getInt("serialNum"),rs.getString("page"));
                carlist.add(car);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return carlist;
    }

    @Override
    public void CarouselSort(int id, int showId) {
        String sql = "UPDATE carousel SET SerialNum = ? WHERE id = ?";
        executeUpdate(sql,showId,id);
    }

    @Override
    public boolean addCarousel(Carousel carousel) {
        String sql = "INSERT INTO carousel VALUES (NULL,?,'#',0,'index')";
        return executeUpdate(sql,carousel.getImage());
    }

    @Override
    public boolean deleteCarousel(int id) {
        String sql = "DELETE FROM carousel WHERE id = ?";
        return executeUpdate(sql, id);
    }

    @Override
    public boolean updateCarousel(Carousel carousel) {
        String sql = "UPDATE carousel SET ahref=? WHERE id = ?";
        return executeUpdate(sql,carousel.getAhref(),carousel.getId());
    }
}
